package com.example.micro.netty.server.nio.buffer;

import java.nio.IntBuffer;
import java.nio.channels.DatagramChannel;

/**
 * 举例说明Buffer的使用
 */
public class BasicBuffer {

    public static void main(String[] args) {

        //先创建一个Buffer,大小为5，即可以存5个int
        IntBuffer intBuffer = IntBuffer.allocate(5);
        for (int i = 0; i < 3; i++) {
            intBuffer.put(i * 2);
        }


        //如何从Buffer中读取数据呢
        //将Buffer转换，读写切换
        intBuffer.flip();
        while (intBuffer.hasRemaining()) {
            System.out.println(intBuffer.get());
            if (intBuffer.position() == 2) {
                break;
            }
        }

        intBuffer.flip();
        System.out.println("position:" + intBuffer.position() + " || limit:" + intBuffer.limit());

        for (int i = 0; i < 3; i++) {
            intBuffer.put(i + 1);
        }

        System.out.println("position:" + intBuffer.position() + " || limit:" + intBuffer.limit());

    }

}
